<template>
  <div style="display: flex;flex-direction: column">
    <div style="display: flex;">
      <span>选择打印机:</span>
      <select :id="'print-select02'" style='border-color:#e4e4e4;max-width:330px' size='1'
              @change='demoCreatePagSizeList'></select>
      <select :id="'paper-select03'" style='border:1px solid #e4e4e4;max-width:330px' size='1' ></select>
    </div>
  </div>
  <button  @click="myBlankDesign">
    空白设计
  </button>
</template>

<script setup>
import {onMounted, ref} from "vue";

onMounted(() => {
  //demoSetClodopJS实现步骤
  // 1.lodop初始化
  // 2.获取打印机列表
  // 3.获取纸张列表
  // 4.
  demoSetClodopJS('http://localhost:8000/CLodopfuncs.js')
})

function demoSetClodopJS(strSrc) {
  var oscript = demoCreateCLodopJSscript(strSrc)
  oscript.onload = oscript.onreadystatechange = function () {
    if ((!oscript.readyState || /loaded|complete/.test(oscript.readyState))){
      //获取打印机列表
      demoCreatePrinterList()
    }
  }
}
 //获取打印机列表
function demoCreatePrinterList() {
  CLODOP.Create_Printer_List(document.getElementById('print-select02'))
  //获取纸张列表
  demoCreatePagSizeList()
}
//获取纸张列表
const demoCreatePagSizeList = (e) => {
  var oSelect = document.getElementById('paper-select03')
  var iPrintIndex = document.getElementById('print-select02').value
  CLODOP.Create_PageSize_List(oSelect, iPrintIndex)
}
//lodop创建初始化
function demoCreateCLodopJSscript(strSrc) {
  var ScriptSS = document.getElementsByTagName('script')
  for (var i in ScriptSS) {
    if (ScriptSS[i] && ScriptSS[i].src && (ScriptSS[i].src.indexOf('CLodopfuncs.js') >= 0)) {
      if (ScriptSS[i].parentNode) ScriptSS[i].parentNode.removeChild(ScriptSS[i])
    }
  }
  var oscript = document.createElement('script')
  if (strSrc.indexOf('src=') >= 0) {
    strSrc = strSrc.match(/=[\',\"][^\',^\"].*(?=[\',\"])/i)
    strSrc = strSrc[0].slice(2)
  }
  oscript.src = strSrc
  var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement
  head.insertBefore(oscript, head.firstChild)
  return oscript
}

const demoPrint = (callback,toPreview) => {
  if(location.href.includes('58.87.103.67')){
    //lisence: https://58.87.103.67/pack/#/
    LODOP.SET_LICENSES("","40D35F1FB09337F66956B67AC7313B23","","");
  } else if(location.href.includes('172.21.94.5')){
    //宁波 http://172.21.94.5/pack/#/ 的lisence
    LODOP.SET_LICENSES("","795625A3F2039CD4B180FE4D5EF72F0C","","");
  } else if(location.href.includes('172.21.94.4')){
    //宁波 http://172.21.94.4/pack/#/ 的lisence
    LODOP.SET_LICENSES("","795625A3F2039BD4B180FE4D5EF62F0C","","");
  } else if(location.href.includes('byun56.com')){
    LODOP.SET_LICENSES("","1566AAE053D1431874D7D0B0F387AA2F44D","",""); //顶级域名 byun56.com
  }
  LODOP.PRINT_INIT()
  LODOP.SET_PRINTER_INDEXA(document.getElementById('print-select02').value)
  callback(LODOP)
  if (toPreview) {
    LODOP.PREVIEW()
  } else {
    LODOP.PRINT()
  }
}

const myBlankDesign = () => {
  LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_空白练习");
  LODOP.PRINT_DESIGN();
};
defineExpose({demoPrint})
</script>

<style scoped>

</style>